package com.etl.extract;

import com.etl.sys.entity.*;
import com.etl.sys.service.*;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-10-21 10:22
 */
@Slf4j
@Component
public class ScheduleTaskBuilder {

    @Autowired
    ISysTaskService sysTaskService;
    @Autowired
    ISysModuleService sysModuleService;
    @Autowired
    ISysCinemaService sysCinemaService;
    @Autowired
    ISysTenantService sysTenantService;
    @Autowired
    ISysDatabaseService sysDatabaseService;
    @Autowired
    ISysScheduleService sysScheduleService;

    @Autowired
    ScheduleBuilder scheduleBuilder;

    @Autowired
    DatabaseBuilder databaseBuilder;

    /**
     * 影院IP替换字符串
     */
    private static final String CINEMA_IP_KEY = "${CINEMA_IP}";
    /**
     * 调度ID与调度任务KEY-VALUE键值对，保存已建造的调度任务
     */
    private static final Map<Long, ScheduleTask> SCHEDULE_TASK_MAP = Maps.newConcurrentMap();

    /**
     * 建造调度任务参数
     *
     * @param sysSchedule 调度记录
     */
    public void buildTaskSchedule(SysSchedule sysSchedule) {
        SysTask sysTask = sysTaskService.getById(sysSchedule.getTaskId());
        SysModule sysModule = sysModuleService.getById(sysSchedule.getModuleId());
        SysCinema sysCinema = sysCinemaService.getById(sysSchedule.getCinemaId());
        SysTenant sysTenant = sysTenantService.getById(sysCinema.getTenantId());
        // 组装调度任务信息
        ScheduleTask scheduleTask = ScheduleTask.builder()
                .schedule(sysSchedule)
                .task(sysTask)
                .module(sysModule)
                .cinema(sysCinema)
                .tenant(sysTenant)
                .build();
        if(Objects.nonNull(sysSchedule.getDatabaseId())){
            SysDatabase sysDatabase = sysDatabaseService.getById(sysSchedule.getDatabaseId());
            // 替换影院IP
            if(Objects.nonNull(sysDatabase)){
                sysDatabase.setJdbcUrl(StringUtils.replace(sysDatabase.getJdbcUrl(), CINEMA_IP_KEY, sysCinema.getCinemaIp()));
            }
            scheduleTask.setDatabase(sysDatabase);
        }
        // 刷新调度任务
        refreshScheduleTask(scheduleTask);
    }

    /**
     * 刷新调度任务
     *
     * @param scheduleTask 调度任务
     */
    public void refreshScheduleTask(ScheduleTask scheduleTask) {
        ScheduleTask originalSchedule = SCHEDULE_TASK_MAP.get(
                scheduleTask.getSchedule().getScheduleId());
        if (Objects.isNull(originalSchedule)) {
            SCHEDULE_TASK_MAP.put(scheduleTask.getSchedule().getScheduleId(), scheduleTask);
            databaseBuilder.loadDatabase(scheduleTask);
            scheduleBuilder.refreshSchedule(scheduleTask);
            return;
        }
        if (!originalSchedule.compareDatabase(scheduleTask.getDatabase())) {
            databaseBuilder.loadDatabase(scheduleTask);
        }
        if (!originalSchedule.compareSchedule(scheduleTask.getSchedule())) {
            scheduleBuilder.refreshSchedule(scheduleTask);
        }
        SCHEDULE_TASK_MAP.put(scheduleTask.getSchedule().getScheduleId(), scheduleTask);
    }

    /**
     * 获取调度任务信息集合
     *
     * @return Collection-ScheduleTask
     */
    public Collection<ScheduleTask> getScheduleTaskList() {
        return SCHEDULE_TASK_MAP.values();
    }
}
